﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using BSDS.Core.Infrastructure.Logging;
using BSDS.Core.Model.Conversion.Configuration;
using BSDS.Core.Model.Data;

namespace BSDS.Core.Model.Conversion.Processing
{
    public static class DeleteColumnsProcessor
    {
        private static DeleteColumnsConfiguration deleteConfiguration;

        public static void LoadConfiguration(string configFile)
        {
            XmlSerializer sr = new XmlSerializer(typeof(DeleteColumnsConfiguration));

            if (File.Exists(configFile))
            {
                using (var f = new FileStream(configFile, FileMode.Open))
                {
                    if (f != null)
                    {
                        deleteConfiguration = sr.Deserialize(f) as DeleteColumnsConfiguration;
                    }
                }
            }
            else
            {
                throw new FileNotFoundException("Unable to open the delete columns configuration file: " + configFile);
            }
        }

        public static void SaveConfiguration(string configFile)
        {
            XmlSerializer sr = new XmlSerializer(typeof(DeleteColumnsConfiguration));

            using (var tr = new StreamWriter(configFile))
            {
                sr.Serialize(tr, deleteConfiguration);
            }
        }

        public static void DeleteColumns(this BsdsData data)
        {
            foreach (var entity in data.Entities)
            {
                foreach (var c in deleteConfiguration.columns)
                {
                    entity.RemoveAttributeValue(c);
                }
            }
        }


        public static void CreateSampleData()
        {
            deleteConfiguration = new DeleteColumnsConfiguration() { columns = new string[3] { "one1", "two1", "three1" }};

            SaveConfiguration("SampleDeleteColumnsConfiguration.xml");
        }
    }
}
